/**
 * @author gw
 */
function changeUi() {
	$('[target=dialog]').each(function() {
		$(this).unbind("click");
		$(this).click(function(event){
			var $this = $(this);
			var title = $this.attr("title") || '标题';
			var width = $this.attr("width") || '1100px';
			var beforeCall = $this.attr("beforeCall");
			if(beforeCall){
				eval(beforeCall);
			}
			var height = $this.attr("height") || '600px';
			var myid = $this.attr("myid");
			var url = $(this).attr("href");
			var msg = $this.attr("msg");
			url = replaceTmById(url,$("#"+myid+"relContentVal"));
			if (!isFinishedTm(url)) {
				layer.msg(msg || "请选择记录");  
				return false;
			}
			layer.open({
	  		    type: 6,
				title:title,
	  		    content: url,
				area: [width, height],
	            anim:2
			    });
			event.preventDefault();
		});
	});
	
	$('[target=ajax]').each(function() {
		$(this).unbind("click");
		$(this).click(function(event){
			var failCount=1;
			var $this = $(this);
			var myid = $this.attr("myid");
			var url = $this.attr("href");
			var close = $this.attr("close");
			var refresh = $this.attr("refresh");
			url = replaceTmById(url,$("#"+myid+"relContentVal"));
			if (!isFinishedTm(url)) {
				layer.msg("请选择记录");  
				return false;
			}
			$.ajax({
		   	    type: "post",
		   	    dataType:"json",
		       // data:data.field,
		        url: url,
		        success: function(json){
		        	layui.use(['table','layer'], function(){
						var  table = layui.table;					        		
						if(data.statusCode=='200'){
							layer.msg("操作成功", { icon: 6, time: 2000 }, function () {
								if(reloadTableId){
									table.reload(reloadTableId, {});
								}
								if(isClose!='null'&&isClose=='true'){
									layer.close(layer.index-failCount);								
								}
							});
						}else{
							layer.msg("操作失败,"+data.message, { icon: 5,time: 2000},function(){					 
								failCount++;
							
							});
						}
					});
		        },
		        error:function(e){
		        	console.info(e);
		        }
	    	});
			event.preventDefault();
		});
	});
	
	$('[target=selectedTodo]').each(function() {
		var $this = $(this);
		$this.unbind("click");
		var selectedIds = $this.attr("rel") || "ids";
		var failCount=1;
		$(this).click(function(event){
			var myid = $this.attr("myid");
			var beforeCall = $this.attr("beforeCall");
			var reloadTableId = $this.attr("reloadTable");
			var isClose=$this.attr("isClose");
			if(beforeCall){
				eval('(' + beforeCall + ')');
			}
			var ids = _getIds(selectedIds, myid);
			if (!ids) {
				layer.msg("请选择记录!");
				return false;
			}
			$.ajax({
				type:'POST', url:$this.attr('href'), dataType:'json', cache: false,
				data: function(){
					var _data = {};
					_data[selectedIds] = ids;
					return _data;
				}(),
				success: function(data) {
					layui.use(['table','layer'], function(){
						var  table = layui.table;					        		
						if(data.statusCode=='200'){
							layer.msg("操作成功", { icon: 6, time: 2000 }, function () {
								if(reloadTableId){
									table.reload(reloadTableId, {});
								}
								if(isClose!='null'&&isClose=='true'){
									layer.close(layer.index-failCount);								
								}
							});
						}else{
							layer.msg("操作失败,"+data.message, { icon: 5,time: 2000},function(){					 
								failCount++;
							
							});
						}
					});

				}
			});
			event.preventDefault();
			return false;
		});
	});
	
	
}
function replaceTmById(url,$parent){
	console.info("url="+url);
	var temp =  url.replace(RegExp("({[A-Za-z_]+[A-Za-z0-9_]*})","g"), function($1){
		var $input = $parent.find("#"+$1.replace(/[{}]+/g, ""));
		return $input.val() ? $input.val() : $1;
	});
	return temp;
}

function isFinishedTm(url){
	return !(new RegExp("{[A-Za-z_]+[A-Za-z0-9_]*}").test(url)); 
}
/**
 * 获取表单ids
 */
function _getIds(selectedIds, myid){
	var ids = "";
	var $box = $("#"+myid+"relCheckBoxVal");
	$box.children("input[name^="+selectedIds+"]").each(function(i){
		var val = $(this).val();
		ids += i==0 ? val : ","+val;
	});
	return ids;
}

/**
 * 表单回调函数
 */
function formCallBack(formId,url) {
	layui.use(['form','layer','table'], function(){
		  var form = layui.form
		  ,table = layui.table
		  ,layer = layui.layer;
		  var failCount=1;
		  //保存费率
		  form.on('submit('+formId+')', function (data) {
				 var oData = new FormData($("#"+formId+"")[0]);
			 	 $.ajax({
				 url:url,
				 type:'post',
				 async: false,  
			     cache: false,  
			     contentType: false,  
			     processData: false,
				 data:oData,
				 dataType:'json',
				 success:function(data){
					 if(data.statusCode=='200'){
						 alert(2222);
						 layer.msg("保存成功", { icon: 6, time: 2000 }, function () {
							 layer.close(layer.index-failCount);
							});
					 }else{
						 layer.msg("保存失败,"+data.message, { icon: 5,time: 2000},function(){
							 failCount++;
						 });
					 }
				   },
				  error:function(msg){
					  alert(msg);
						  console.info(msg);
						  
				  }
			     });
			 	 return false;
			  });
		  form.render();  
		});
}

/**
 * 得到数据表格选中的值
 * @author yehao
 * @returns {___anonymous5297_5298}
 */
function  getSelectRecord(tableId){
	var record = {},isNoEmpty = false;
	$("#"+tableId+"relContentVal").find(":hidden").each(function (){
		record[$(this).attr("id")] = $(this).val();
		isNoEmpty = true;
	});
	if(isNoEmpty){
		return record;
	}
}

window.mtdl = {
		ajax : function (settings){
			if (typeof settings.success === 'undefined') {
				settings.success = function(data, textStatus, jqXHR) {
					if(data.statusCode == "200"){
						layer.alert(data.message, { icon: 6,shadeClose: true });
						if(settings.afterSuccess != 'undefined'){
							settings.afterSuccess(data, textStatus, jqXHR);
						}
					}else{
						layer.alert(data.message, { icon: 5,shadeClose: true });
					}
				}
			}
			if (typeof settings.error === 'undefined') {
				settings.error = function(jqXHR, textStatus, errorThrown) {
					layer.alert(errorThrown, { icon: 5,shadeClose: true });
					if(settings.afterError != 'undefined'){
						settings.afterError(jqXHR, textStatus, errorThrown);
					}
				}
			
			}
			$.ajax(settings);
		},
		isBlank : function (o){
			if(typeof o == 'string'){
				return o == null || o == '' || o.replace(/\s*/g,'') == '';
			}
			return o == null || o == '' || (o instanceof Array && o.length == 0);
		},trim: function(str) {
	    	str = str.replace(/^\s\s*/, '');
	    	
	    	var ws = /\s/, i = str.length;
	    	while (ws.test(str.charAt(--i)));
	    	return str.slice(0, i + 1);
	    }
	}


